﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace TorneoLoL
{
    class Conexion
    {

        public bool consultarDataSet( ref DataSet ds, string NombreTabla , string Consulta )
        {
            String[] ruta = new String[]{"Data Source=localhost\\sqlexpress;Initial Catalog=Torneo;Integrated Security=True",
                                        "Data Source=DESKTOP-I05G4V3;Initial Catalog=Torneo;Integrated Security=True"};
            string ConsultaSQL = Consulta;
            try
            {
                SqlConnection cn = new SqlConnection( ruta[0] );
                SqlDataAdapter adaptador = new SqlDataAdapter( ConsultaSQL, cn );
                cn.Open();
                adaptador.Fill( ds, NombreTabla );
                cn.Close();
                return true;
            }
            catch(SqlException)
            {
                try
                {
                    SqlConnection cn = new SqlConnection( ruta[1] );
                    SqlDataAdapter adaptador = new SqlDataAdapter( ConsultaSQL, cn );
                    cn.Open();
                    adaptador.Fill( ds, NombreTabla );
                    cn.Close();
                    return true;
                }
                catch(SqlException err)
                {
                    System.Windows.Forms.MessageBox.Show( "Posible error con servicio SQL. " /*+ System.Windows.Forms.Message.NewLine */+ err.Message /*+ Environment.NewLine +*/ ," Error. El programa se cerrará", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error );
                    Environment.Exit( 1 );
                }
            }
            return false;
        }
        private void errorSP(string error)
        {
            MessageBox.Show("Error al ejecutar el SP7" /*+ Environment.NewLine */+ error /*+ Environment.NewLine */+ "El programa se cerrará.");
        }
        /*    public void EjecutarSP( string nombreP, SqlCommand comando , ref DataSet DS)
            {
                String[] ruta = new String[]{"Data Source=localhost\\sqlexpress;Initial Catalog=Torneo;Integrated Security=True",
                                            "Data Source=DESKTOP-I05G4V3;Initial Catalog=Torneo;Integrated Security=True"};



                try
                {
                    SqlConnection Conexion = new SqlConnection(ruta[0]);
                    Conexion.Open();
                    SqlCommand cmd = new SqlCommand(nombreP, Conexion);

                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    //cmd.ExecuteReader();
                    adapter.Fill(DS, nombreP);
                    Conexion.Close();

                }
                catch (SqlException)
                {
                    try
                    {
                        SqlConnection Conexion = new SqlConnection(ruta[1]);
                        Conexion.Open();
                        SqlCommand cmd = new SqlCommand(nombreP, Conexion);

                        cmd.CommandType = CommandType.StoredProcedure;
                        SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                       // cmd.ExecuteReader();
                        adapter.Fill(DS,nombreP);
                        Conexion.Close();

                    }
                    catch (System.Exception err)
                    {
                            errorSP(err.Message);
                            Environment.Exit(1);                                    
                    }

                }


            }
            */
    }
}

